import 'package:expandable_text/expandable_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/res/lf_double_ext.dart';
import 'package:largefriends/res/lf_string_ext.dart';

import '../../../entity/entities/chat_new_message_entity.dart';
import '../../../res/lf_color.dart';
import '../../../res/lf_fonts.dart';
import '../../../res/lf_text_style.dart';
import '../../../res/strings/lf_strings.dart';
import '../../../service/lf_auth_service.dart';
import 'LfMessageMenu.dart';

class LfMessageReply extends StatelessWidget {
  final ChatNewMessageEntity data;
  final Function() recall;
  final Function() report;
  final Function() reply;
  final bool canShowMenu;
  const LfMessageReply({
    Key? key,
    required this.data,
    required this.recall,
    required this.report,
    required this.reply,
    required this.canShowMenu,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    LfAuthService authService = Get.find();
    var isSender =
        data.sender?.profile?.userId == authService.currentAccount!.userId;
    var replyContent = data.replyInfo?.content ?? '';
    if (replyContent.trim().isEmpty) {
      replyContent = '[${strUnsent.tr}]';
    }

    bool length = (data.message?.replaceBreak?.length ?? 0) >
        (replyContent.replaceBreak?.length ?? 0);

    var text = Container(
      padding: length ? EdgeInsets.only(top: 2.h) : null,
      decoration: length
          ? BoxDecoration(
              border: Border(
                top: BorderSide(
                    width: 1.5.h,
                    color:
                        isSender ? Colors.white.withAlpha(100) : Colors.white.withAlpha(156)),
              ),
            )
          : null,
      child: ExpandableText(
          data.message?.replaceBreak.replaceWord(isSender) ?? "",
          expandText: '${strMore.tr}>>',
          collapseText: '<<${strLess.tr}',
          expandOnTextTap: true,
          animation: true,
          collapseOnTextTap: false,
          linkColor: colorFEFEFE,
          linkEllipsis: false,
          maxLines: 6,
          // position:
          style: lfTextStyle(
              sp: 15.sp,
              fontFamily: fontRegular,
              color: isSender ? colorFEFEFE:colorFEFEFE)),
    );

    var replyWidget = Container(
        padding: (!length) ? EdgeInsets.only(bottom: 2.h) : null,
        decoration: (!length)
            ? BoxDecoration(
                border: Border(
                  bottom: BorderSide(
                      width: 1.h,
                      color:
                          isSender ? Colors.white.withAlpha(100) : Colors.white.withAlpha(100)),
                ),
              )
            : null,
        child: ExpandableText(
            replyContent.replaceBreak.replaceWord(isSender) ?? "",
            expandText: '${strMore.tr}>>',
            collapseText: '<<${strLess.tr}',
            expandOnTextTap: true,
            animation: true,
            collapseOnTextTap: false,
            linkColor: color210215,
            linkEllipsis: false,
            maxLines: 2,
            // position:
            style: lfTextStyle(
                sp: 14.sp,
                fontFamily: fontRegular,
                color: isSender ? Colors.white.withAlpha(156) : Colors.white.withAlpha(156))));
    if (isSender ) {
      return Row(
        mainAxisAlignment: MainAxisAlignment.end,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Flexible(
              child: getMenuWidget(
                  data: data,
                  child: Container(
                    padding:
                        EdgeInsets.symmetric(vertical: 8.h, horizontal: 10.w),
                    decoration: BoxDecoration(
                      color: color514F62,
                      borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(10.r),
                          bottomRight: Radius.circular(10.r),
                          bottomLeft: Radius.circular(10.r)),
                    ),
                    constraints: BoxConstraints(
                        maxWidth: ScreenUtil().screenWidth - 70.w,
                        minHeight: 42.h),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [replyWidget, text],
                    ),
                  ),
                  isSender: isSender,
                  recall: recall,
                  report: report,
                  reply: reply,
                  canShowMenu: canShowMenu)),
          10.w.sbw
        ],
      );
    } else {
      return Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          10.w.sbw,
          // CustomCacheImage(
          //   imageUrl: data.sender?.profile?.avatarUrl,
          //   isCircle: true,
          //   width: 40.r,
          //   height: 40.r,
          // ),
          // 5.w.sbw,
          Flexible(
              child: Container(
            padding: EdgeInsets.symmetric(vertical: 8.h, horizontal: 10.w),
            decoration: BoxDecoration(
              color: color8F8F8F,
              borderRadius: BorderRadius.only(
                  topRight: Radius.circular(10.r),
                  bottomRight: Radius.circular(10.r),
                  bottomLeft: Radius.circular(10.r)),
            ),
            constraints:
                BoxConstraints(maxWidth: ScreenUtil().screenWidth - 70.w),
            child: text,
          )),
          10.w.sbw
        ],
      );
    }
  }
}
